package com.example.demo.controller;


import com.example.demo.bean.VesselBean;
import com.example.demo.repository.secondary.VesselRepository;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by dongjunpeng on 2017/10/12.
 */
@RequestMapping("/vessels")
@RestController
@Api(tags = "Vessel", description = "船舶")
public class VesselController {

    private static final Logger logger = LoggerFactory.getLogger(VesselController.class);

    @Autowired
    private VesselRepository vesselViewRepository;


    @ApiOperation("查询单个船舶")
    @GetMapping("/{vessel_code}")
    public Object findVesselById(@PathVariable("vessel_code")String vesselCode){
        StringBuffer sb = new StringBuffer();
        sb.append("SELECT vsl.vessel_code, vsl.vessel_name_cn, vsl.vessel_name_en,");
        sb.append(" vsl.operator_code, vsl.agent, vsl.vessel_type, vsl.length_over_all, vsl.length_between_perpendiculars,");
        sb.append(" vsl.vessel_width, vsl.imo, vsl.mmsi, vsl.call_sign, vsl.flag, vsl.bow_to_bridge,");
        sb.append(" vsl.stern_to_bridge, vsl.net_tonnage, vsl.gross_tonnage, vsl.summer_draft, vsl.max_capacity,");
        sb.append(" vsl.deck_max_tier, vsl.hold_max_tier, vsl.deck_max_row, vsl.hold_max_row");
        sb.append(" FROM outerlink_vessel vsl");
        sb.append(" WHERE vsl.vessel_code='" + vesselCode + "'");
        List<Object[]> re = this.vesselViewRepository.listBySQL(sb.toString());
        List<VesselBean> vesselBeanList = new ArrayList<>();
        for (Object[] objects : re){
            VesselBean vesselBean = new VesselBean();
            vesselBean.setVesselCode(objects[0] == null ? null : objects[0].toString());
            vesselBean.setVesselNameCn(objects[1] == null ? null : objects[1].toString());
            vesselBean.setVesselNameEn(objects[2] == null ? null : objects[2].toString());
            vesselBean.setOperatorCode(objects[3] == null ? null : objects[3].toString());
            vesselBean.setAgent(objects[4] == null ? null : objects[4].toString());
            vesselBean.setVesselType(objects[5] == null ? null : objects[5].toString());
            vesselBean.setLengthOverAll(objects[6] == null ? null : (BigDecimal) objects[6]);
            vesselBean.setLengthBetweenPerpendiculars(objects[7] == null ? null : (BigDecimal) objects[7]);
            vesselBean.setVesselWidth(objects[8] == null ? null : (BigDecimal) objects[8]);
            vesselBean.setImo(objects[9] == null ? null : objects[9].toString());
            vesselBean.setMmsi(objects[10] == null ? null : objects[10].toString());
            vesselBean.setCallSign(objects[11] == null ? null : objects[11].toString());
            vesselBean.setFlag(objects[12] == null ? null : objects[12].toString());
            vesselBean.setBowToBridge(objects[13] == null ? null : (BigDecimal) objects[13]);
            vesselBean.setSternToBridge(objects[14] == null ? null : (BigDecimal) objects[14]);
            vesselBean.setNetTonnage(objects[15] == null ? null : (BigDecimal) objects[15]);
            vesselBean.setGrossTonnage(objects[16] == null ? null : (BigDecimal) objects[16]);
            vesselBean.setSummerDraft(objects[17] == null ? null : (BigDecimal) objects[17]);
            vesselBean.setMaxCapacity(objects[18] == null ? null : (BigDecimal) objects[18]);
            vesselBean.setDeckMaxTier(objects[19] == null ? null : (BigDecimal) objects[19]);
            vesselBean.setHoldMaxTier(objects[20] == null ? null : (BigDecimal) objects[20]);
            vesselBean.setDeckMaxRow(objects[21] == null ? null : (BigDecimal) objects[21]);
            vesselBean.setHoldMaxRow(objects[22] == null ? null : (BigDecimal) objects[22]);
            vesselBeanList.add(vesselBean);
        }
        return vesselBeanList;
    }

    /*@ApiOperation("查询单船所有集装箱")
    @GetMapping("/{vessel_voyage_number}/containers")
    public Object findContainerByVesselId(@PathVariable("vessel_voyage_number")String vesselVoyageNumber){
        StringBuffer sb = new StringBuffer();
        sb.append("SELECT c.container_id, c.container_number, c.size_type, c.stuffing_status, c.transfer_type,");
        sb.append(" c.trade_type, c.container_type, c.cargo_type, c.operator_code, c.bl_number, c.booking_number,");
        sb.append(" c.vessel_code, c.vessel_voyage_number, c.in_voyage, c.out_voyage, c.container_status,");
        sb.append(" c.yard_position, c.vessel_position, c.job_type, c.port_of_loading, c.port_of_discharge,");
        sb.append(" c.port_of_distination, c.weight, c.seal_number, c.sequence_number, c.yard_truck_name,");
        sb.append(" c.crane_name, c.order_time, c.finish_time FROM container_view c");
        sb.append(" WHERE c.vessel_voyage_number='" + vesselVoyageNumber + "'");
        List<Object[]> re = this.containerViewRepository.listBySQL(sb.toString());
        List<ContainerBean> containerBeanList = new ArrayList<>();
        for (Object[] objects : re){
            ContainerBean containerBean = new ContainerBean();
            containerBean.setContainerId((int) objects[0]);
            containerBean.setContainerNumber(objects[1] == null ? null : objects[1].toString());
            containerBean.setSizeType(objects[2] == null ? null : objects[2].toString());
            containerBean.setStuffingStatus(objects[3] == null ? null : objects[3].toString());
            containerBean.setTransferType(objects[4] == null ? null : objects[4].toString());
            containerBean.setTradeType(objects[5] == null ? null : objects[5].toString());
            containerBean.setContainerType(objects[6] == null ? null : objects[6].toString());
            containerBean.setCargoType(objects[7] == null ? null : objects[7].toString());
            containerBean.setOperatorCode(objects[8] == null ? null : objects[8].toString());
            containerBean.setBlNumber(objects[9] == null ? null : objects[9].toString());
            containerBean.setBookingNumber(objects[10] == null ? null : objects[10].toString());
            containerBean.setVesselCode(objects[11] == null ? null : objects[11].toString());
            containerBean.setVesselVoyageNumber(objects[12] == null ? null : objects[12].toString());
            containerBean.setInVoyage(objects[13] == null ? null : objects[13].toString());
            containerBean.setOutVoyage(objects[14] == null ? null : objects[14].toString());
            containerBean.setContainerStatus(objects[15] == null ? null : objects[15].toString());
            containerBean.setYardPosition(objects[16] == null ? null : objects[16].toString());
            containerBean.setVesselPosition(objects[17] == null ? null : objects[17].toString());
            containerBean.setJobType(objects[18] == null ? null : objects[18].toString());
            containerBean.setPortOfLoading(objects[19] == null ? null : objects[19].toString());
            containerBean.setPortOfDischarge(objects[20] == null ? null : objects[20].toString());
            containerBean.setPortOfDistination(objects[21] == null ? null : objects[21].toString());
            containerBean.setWeight(objects[22] == null ? null : (Short) objects[22]);
            containerBean.setSealNumber(objects[23] == null ? null : objects[23].toString());
            containerBean.setSequenceNumber(objects[24] == null ? null : objects[24].toString());
            containerBean.setYardTruckName(objects[25] == null ? null : objects[25].toString());
            containerBean.setCraneName(objects[26] == null ? null : objects[26].toString());
            containerBean.setOrderTime(objects[27] == null ? null : objects[27].toString());
            containerBean.setFinishTime(objects[28] == null ? null : objects[28].toString());
            containerBeanList.add(containerBean);
        }
        return APIUtils.toMap(UtilHttpStatus.OK.value(), true, containerBeanList);
    }*/

}
